package test5.lupeng.jf.com.teamdemo.activitys;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.adapter.BlogCommentAdapter;
import test5.lupeng.jf.com.teamdemo.bean.request.BlogCommentBean;
import test5.lupeng.jf.com.teamdemo.bean.response.BlogCommentResponse;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;

public class MoreBlogCommentActivity extends AppCompatActivity {
    private static final String TAG = "MoreBlogCommentActivity";
    ArrayList<BlogCommentResponse.ResultBean> mResultBeen = new ArrayList<>();
    private BlogCommentAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_blog_comment);
        String blogId= getIntent().getStringExtra("id_blog");
        Log.e(TAG, "onCreate: "+blogId );
        initView();
        getData(blogId,0,0);
    }


    /**
     * 初始化视图
     */
    private void initView(){
        ImageView iv_back= (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        RecyclerView rl_more_blog= (RecyclerView) findViewById(R.id.rl_more_blog);
        rl_more_blog.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BlogCommentAdapter(mResultBeen);
        rl_more_blog.setAdapter(mAdapter);
    }

    /**
     * 获取评论
     * @param articleId  博客id
     * @param limit       条数
     * @param timestamp    时间戳
     */
    private void getData(String articleId, int limit, int timestamp) {
        String url = "https://leancloud.cn:443/1.1/functions/getBlogComments";
        BlogCommentBean bean = new BlogCommentBean(articleId, limit, timestamp);
        Request request = HTTPUtils.requestPost(bean, url);
        OkHttpClient client = new OkHttpClient();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonResult = response.body().string();
                Log.e(TAG, "onResponse: " + jsonResult);
                if(response.code()==200){
                    BlogCommentResponse blogCommentResponse = new Gson().fromJson(jsonResult, BlogCommentResponse.class);
                    mResultBeen.clear();
                    mResultBeen.addAll(blogCommentResponse.getResult());

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mAdapter.notifyDataSetChanged();
                        }
                    });

                }
            }
        });
    }
}
